package cn.ctodb.mall.goods.web.rest;

import com.codahale.metrics.annotation.Timed;
import cn.ctodb.mall.goods.service.GoodsTypeService;
import cn.ctodb.mall.goods.web.rest.util.HeaderUtil;
import cn.ctodb.mall.goods.web.rest.util.PaginationUtil;
import cn.ctodb.mall.goods.service.dto.GoodsTypeDTO;
import io.swagger.annotations.ApiParam;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing GoodsType.
 */
@RestController
@RequestMapping("/api")
public class GoodsTypeResource {

    private final Logger log = LoggerFactory.getLogger(GoodsTypeResource.class);

    private static final String ENTITY_NAME = "goodsType";
        
    private final GoodsTypeService goodsTypeService;

    public GoodsTypeResource(GoodsTypeService goodsTypeService) {
        this.goodsTypeService = goodsTypeService;
    }

    /**
     * POST  /goods-types : Create a new goodsType.
     *
     * @param goodsTypeDTO the goodsTypeDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new goodsTypeDTO, or with status 400 (Bad Request) if the goodsType has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/goods-types")
    @Timed
    public ResponseEntity<GoodsTypeDTO> createGoodsType(@RequestBody GoodsTypeDTO goodsTypeDTO) throws URISyntaxException {
        log.debug("REST request to save GoodsType : {}", goodsTypeDTO);
        if (goodsTypeDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new goodsType cannot already have an ID")).body(null);
        }
        GoodsTypeDTO result = goodsTypeService.save(goodsTypeDTO);
        return ResponseEntity.created(new URI("/api/goods-types/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /goods-types : Updates an existing goodsType.
     *
     * @param goodsTypeDTO the goodsTypeDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated goodsTypeDTO,
     * or with status 400 (Bad Request) if the goodsTypeDTO is not valid,
     * or with status 500 (Internal Server Error) if the goodsTypeDTO couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/goods-types")
    @Timed
    public ResponseEntity<GoodsTypeDTO> updateGoodsType(@RequestBody GoodsTypeDTO goodsTypeDTO) throws URISyntaxException {
        log.debug("REST request to update GoodsType : {}", goodsTypeDTO);
        if (goodsTypeDTO.getId() == null) {
            return createGoodsType(goodsTypeDTO);
        }
        GoodsTypeDTO result = goodsTypeService.save(goodsTypeDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, goodsTypeDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /goods-types : get all the goodsTypes.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of goodsTypes in body
     */
    @GetMapping("/goods-types")
    @Timed
    public ResponseEntity<List<GoodsTypeDTO>> getAllGoodsTypes(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of GoodsTypes");
        Page<GoodsTypeDTO> page = goodsTypeService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/goods-types");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /goods-types/:id : get the "id" goodsType.
     *
     * @param id the id of the goodsTypeDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the goodsTypeDTO, or with status 404 (Not Found)
     */
    @GetMapping("/goods-types/{id}")
    @Timed
    public ResponseEntity<GoodsTypeDTO> getGoodsType(@PathVariable Long id) {
        log.debug("REST request to get GoodsType : {}", id);
        GoodsTypeDTO goodsTypeDTO = goodsTypeService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(goodsTypeDTO));
    }

    /**
     * DELETE  /goods-types/:id : delete the "id" goodsType.
     *
     * @param id the id of the goodsTypeDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/goods-types/{id}")
    @Timed
    public ResponseEntity<Void> deleteGoodsType(@PathVariable Long id) {
        log.debug("REST request to delete GoodsType : {}", id);
        goodsTypeService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/goods-types?query=:query : search for the goodsType corresponding
     * to the query.
     *
     * @param query the query of the goodsType search 
     * @param pageable the pagination information
     * @return the result of the search
     */
    @GetMapping("/_search/goods-types")
    @Timed
    public ResponseEntity<List<GoodsTypeDTO>> searchGoodsTypes(@RequestParam String query, @ApiParam Pageable pageable) {
        log.debug("REST request to search for a page of GoodsTypes for query {}", query);
        Page<GoodsTypeDTO> page = goodsTypeService.search(query, pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/goods-types");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }


}
